﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace LS.Common
{
    public class HttpHelper
    {

        public static string GetHttpResponse(string url, string method, string postData = "", string header = "")
        {
            string result = string.Empty;
            try
            {
                ServicePointManager.DefaultConnectionLimit = 100;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = method;
                req.Timeout = 8000;//设置请求超时时间，单位为毫秒
                req.ContentType = "application/json";
                req.KeepAlive = false;
                if (!string.IsNullOrEmpty(header))
                {
                    req.Headers.Add(HttpRequestHeader.Cookie, header);
                }
                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] data = Encoding.UTF8.GetBytes(postData);
                    req.ContentLength = data.Length;
                    using (Stream reqStream = req.GetRequestStream())
                    {
                        reqStream.Write(data, 0, data.Length);
                        reqStream.Close();
                    }
                }
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                req.Abort();
                resp.Close();
            }
            catch(Exception ex)
            {
                return "-1";
            }
            return result;
        }

        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string HttpPost(string url, string param, List<header> list)
        {
            string strValue = "";
            #region  xx
            try
            {
                string strURL = url;
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST";
                request.ContentType = "application/json";
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        request.Headers[item.keyname] = item.keycode;
                    }
                }

                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(param);
                request.ContentLength = payload.Length;
                System.IO.Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                var response = request.GetResponse();
                System.IO.Stream s;
                s = response.GetResponseStream();
                string StrDate = "";

                StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                while ((StrDate = Reader.ReadLine()) != null)
                {
                    strValue += StrDate + "\r\n";
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.GetLogger().Error(ex.StackTrace + ex.Message);
            }
            return strValue;
            #endregion
        }

        public class header
        {
            public string keyname { get; set; }
            public string keycode { get; set; }
        }
    }
}
